<?php

/*
 * File name : checklogin.php
 * Description : Checks if the user already exists. If not creates the login account.
 * Created By : Pratibha Awasthi.
 */
include_once "basePage.php";

class CheckLogin extends basePage{
	
	// Checks if the subscriber already has an acoount and if not creates on.
	public function check_and_insert(){
		
		$fname = $_POST['firstname'];
		$midname = $_POST['midname'];
		$lastname = $_POST['lastname'];
		$password = $_POST['password'];
		$userid = $_POST['userid'];
		
		$con = mysql_connect('localhost','pa_user','secret_password');
			if (!$con){
		  		die('Could not connect: ' . mysql_error());
		  	}
		  
			mysql_select_db("TGS_GM_RESERVATION", $con);
    	    
			// Check if the user id and password detals already exist in the database
			$result = mysql_query("SELECT UserID FROM TGS_GM_RESERVATION.tblsubscriber WHERE FirstName = '{$fname}' AND Middlename = '{$midname}' AND LastName = '{$lastname}' AND UserID = '{$userid}'");
		 	$row = mysql_fetch_array($result);
		 	
		 	if ($row['UserID'] == NULL){
		 		mysql_query("INSERT INTO TGS_GM_RESERVATION.tblsubscriber(FirstName ,MiddleName, LastName,UserID, UserPassword) VALUES ('{$fname}', '{$midname}', '{$lastname}', '{$userid}','{$password}')");
		 		$result = mysql_query("SELECT UserID FROM TGS_GM_RESERVATION.tblsubscriber WHERE FirstName = '{$fname}' AND Middlename = '{$midname}' AND LastName = '{$lastname}' AND UserID = '{$userid}'");

		 		echo <<<ESIGNUP
				<form name="check_login" method="post" action="">
				<h3> Account Successfully Created !!! </h3>
				<p>
		           <input type = 'submit' name = 'submit' value = 'Back' onclick='history.go(-1);'>
		        </p>		
				</form>
ESIGNUP;

		 	}
		 	//  Proper message is shown to the user in case the account alreday exists
		 	else {
		 		echo '<h3> Account already exists !!!';
		 	}
		
	}
	
}

$page = new CheckLogin();
$page->header();
$page->topmenu_login();
$page->check_and_insert();
$page->footer();

?>